package com.aaron.kjviewutils.stock;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.BaseBean;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.aaron.kjviewutils.car.CarInfoAdapter;
import com.aaron.kjviewutils.car.CarInfoBean;
import com.aaron.kjviewutils.driving.DrivingDocActivity;
import com.aaron.kjviewutils.driving.DrivingDocBean;
import com.aaron.kjviewutils.glide.GlideCircleTransform;
import com.aaron.kjviewutils.spinkit.PoponDismissListener;
import com.bumptech.glide.Glide;

/**
 * Created by Aaron on 2017/3/2.
 */

public class MsgPopWin extends PopupWindow {

    private Context context;
    private Activity aty;

    public
    MsgPopWin ( Context context, Activity aty) {
        this.context = context;
        this.aty = aty;
    }

    public
    void showUi (BaseBean base, MsgType type) {
        View view = null;
        switch (type)
        {
            case MSG_STOCK:
            {
                view = LayoutInflater.from ( context ).inflate (
                        R.layout.pop_msg,
                        null
                );
                StockBean.StockInner stock = (StockBean.StockInner) base;
                TextView name = (TextView) view.findViewById(R.id.name);
                TextView code = (TextView) view.findViewById(R.id.code);
                TextView price = (TextView) view.findViewById(R.id.price);
                RelativeLayout sureL = (RelativeLayout) view.findViewById(R.id.sureL);
                name.setText("股票名称："+stock.name);
                code.setText("股票代号："+stock.code);
                price.setText("实时价格：¥"+stock.price);
                sureL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismissView();
                    }
                });
            }
            break;
            case MSG_CARS_INFO:
            {
                view = LayoutInflater.from ( context ).inflate (
                        R.layout.pop_msg_car,
                        null
                );
                CarInfoBean carInfo = (CarInfoBean) base;
                TextView carTips = (TextView) view.findViewById(R.id.carTips);
                ImageView closeBtn = (ImageView) view.findViewById(R.id.closeBtn);
                RecyclerView carRecyclerView = (RecyclerView) view.findViewById(R.id.carRecyclerView);
                if(null!=carInfo&&null!=carInfo.result&&!carInfo.result.isEmpty())
                {
                    carTips.setText(carInfo.result.get(0).brandName);
                    closeBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismissView();
                        }
                    });
                    //构建recycler
                    // 错列网格布局
                    carRecyclerView.setLayoutManager(new LinearLayoutManager(aty));
                    CarInfoAdapter adapter = new CarInfoAdapter(aty, carInfo.result);
                    carRecyclerView.setAdapter(adapter);
                    carRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
                    carRecyclerView.addItemDecoration(new RecycleViewDividerForList(aty, LinearLayoutManager.HORIZONTAL));
                }
            }
            break;
            case MSG_DRIVING_DOC:
            {
                //驾考答题
                view = LayoutInflater.from ( context ).inflate (
                        R.layout.pop_msg_driving_doc,
                        null
                );
                DrivingDocBean.DrivingDocInfo.DrivingDocInner drivingDoc = (DrivingDocBean.DrivingDocInfo.DrivingDocInner) base;
                TextView drivingDocTitle = (TextView) view.findViewById(R.id.drivingDocTitle);
                LinearLayout answersL = (LinearLayout) view.findViewById(R.id.answersL);
                ImageView docImage = (ImageView) view.findViewById(R.id.docImage);
                TextView answer = (TextView) view.findViewById(R.id.answer);
                TextView explainText = (TextView) view.findViewById(R.id.explainText);
                ImageView closeBtn = (ImageView) view.findViewById(R.id.closeBtn);
                drivingDocTitle.setText(drivingDoc.title);
                if(!TextUtils.isEmpty(drivingDoc.file))
                {
                    if(drivingDoc.file.endsWith(".jpg")||drivingDoc.file.endsWith(".png"))
                    {
                        Glide.with(aty).load(drivingDoc.file).transform(new GlideCircleTransform(aty)).animate(android.R.anim.slide_in_left).placeholder(R.mipmap.ic_launcher).into(docImage);
                    }
                }
                else
                {
                    docImage.setVisibility(View.GONE);
                }
                answersL.removeAllViews();
                if(!TextUtils.isEmpty(drivingDoc.a))
                {
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.topMargin = 10;
                    lp.bottomMargin = 10;
                    TextView tag = new TextView(aty);
                    tag.setText("A："+drivingDoc.a);
                    tag.setTextSize(((DrivingDocActivity)aty).px2dp(aty.getResources().getDimension(R.dimen.tips)));
                    tag.setTextColor(aty.getResources().getColor(R.color.tips));
                    tag.setLayoutParams(lp);
                    answersL.addView(tag);
                }
                if(!TextUtils.isEmpty(drivingDoc.b))
                {
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.topMargin = 10;
                    lp.bottomMargin = 10;
                    TextView tag = new TextView(aty);
                    tag.setText("B："+drivingDoc.b);
                    tag.setTextSize(((DrivingDocActivity)aty).px2dp(aty.getResources().getDimension(R.dimen.pickerview_opt)));
                    tag.setTextColor(aty.getResources().getColor(R.color.tips));
                    tag.setLayoutParams(lp);
                    answersL.addView(tag);
                }
                if(!TextUtils.isEmpty(drivingDoc.c))
                {
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.topMargin = 10;
                    lp.bottomMargin = 10;
                    TextView tag = new TextView(aty);
                    tag.setText("C："+drivingDoc.c);
                    tag.setTextSize(((DrivingDocActivity)aty).px2dp(aty.getResources().getDimension(R.dimen.pickerview_opt)));
                    tag.setTextColor(aty.getResources().getColor(R.color.tips));
                    tag.setLayoutParams(lp);
                    answersL.addView(tag);
                }
                if(!TextUtils.isEmpty(drivingDoc.d))
                {
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.topMargin = 10;
                    lp.bottomMargin = 10;
                    TextView tag = new TextView(aty);
                    tag.setText("D："+drivingDoc.d);
                    tag.setTextSize(((DrivingDocActivity)aty).px2dp(aty.getResources().getDimension(R.dimen.pickerview_opt)));
                    tag.setTextColor(aty.getResources().getColor(R.color.tips));
                    tag.setLayoutParams(lp);
                    answersL.addView(tag);
                }
                if("select".equals(drivingDoc.tikuType))
                {
                    switch (drivingDoc.val)
                    {
                        case "1":
                        {
                            answer.setText("a");
                        }
                        break;
                        case "2":
                        {
                            answer.setText("b");
                        }
                        break;
                        case "3":
                        {
                            answer.setText("c");
                        }
                        break;
                        case "4":
                        {
                            answer.setText("d");
                        }
                        break;
                        default:
                        {
                            answer.setText(drivingDoc.val);
                        }
                        break;
                    }
                }
                else
                {
                    switch (drivingDoc.val)
                    {
                        case "1":
                        {
                            answer.setText("正确");
                        }
                        break;
                        case "0":
                        {
                            answer.setText("错误");
                        }
                        break;
                        default:
                        {
                            answer.setText(drivingDoc.val);
                        }
                        break;
                    }
                }
                explainText.setText(drivingDoc.explainText);
                closeBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismissView();
                    }
                });
            }
            break;
            default:
                break;
        }

        // 设置SelectPicPopupWindow的View
        this.setContentView ( view );
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth (WindowManager.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight (WindowManager.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        BaseActivity.backgroundAlpha(aty, 0.4f);
    }

    public void dismissView()
    {
        setOnDismissListener ( new PoponDismissListener( aty ) );
        dismiss ();
    }

    public enum MsgType
    {
        MSG_STOCK,
        MSG_CARS_INFO,
        MSG_DRIVING_DOC;
    }
}
